package com.xmh.huaWeiCloud.controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.obs.services.exception.ObsException;
import com.xmh.huaWeiCloud.dao.VideoDao;
import com.xmh.huaWeiCloud.vo.VideoVo;
import com.xmh.util.HuaWeiCloudFileUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/fileApi/video")
public class FileController {
	
	@Autowired
	private VideoDao videoDao;

	@RequestMapping("/playPage")
	public ModelAndView spendingPlanCount(@RequestParam("videoKey") String videoKey) {
		ModelAndView mView = new ModelAndView();
		mView.setViewName("pages/video");
		mView.addObject("videoKey", videoKey);
		return mView;
	}
	
	@RequestMapping("/uploadFilePage")
	public ModelAndView uploadFilePage() {
		ModelAndView mView = new ModelAndView();
		mView.setViewName("pages/File");
		return mView;
	}

	@RequestMapping(value = "/uploadFile", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("price") String price)
			throws FileNotFoundException, IOException {
		// 获取文件名
		String fileName = file.getOriginalFilename();

		String objectKey = System.currentTimeMillis() + "_" + fileName;

		String hotBucketName = HuaWeiCloudFileUtil.getHotBucketName();
		
		VideoVo videoVo = new VideoVo();
		videoVo.setName(fileName);
		videoVo.setObjectKey(objectKey);
		videoVo.setPrice(new BigDecimal(price));
		videoVo.setSize(file.getSize() / 1024L / 1024L);
		videoVo.setCreateDate(new Date());
		videoVo.setUpdateDate(new Date());
		videoVo.setBucketName(hotBucketName);
		
		videoDao.addVideo(videoVo);

		HuaWeiCloudFileUtil.uploadFile(hotBucketName, objectKey, file.getInputStream());
		

		return "ok";
	}

	@GetMapping("/play")
	public Map<String, Object> getStream(@RequestParam("videoKey") String objectKey, HttpServletResponse response)
			throws IOException {
		try {
			OutputStream outputStream = response.getOutputStream();

			byte[] fileBytes = HuaWeiCloudFileUtil.getFileBytes(HuaWeiCloudFileUtil.getHotBucketName(), objectKey);

			outputStream.write(fileBytes);

			return null;
		} catch (ObsException e) {
			log.error("Response Code: " + e.getResponseCode());
			log.error("Error Message: " + e.getErrorMessage());
			log.error("Error Code:       " + e.getErrorCode());
			log.error("Request ID:      " + e.getErrorRequestId());
			log.error("Host ID:           " + e.getErrorHostId());
		}
		return null;
	}

}
